Mg {
	classvar midiChan;
	classvar strips;
	classvar channels;
	classvar white;
	classvar serial;
	classvar melodic;
	classvar net;
	classvar testing;
	classvar lastTouched;

	*run{
		var messages = [ "tempo", "shorten", "stutter",
			"amplitude", "accident", "chord" ];
		lastTouched = 0;
		testing = Platform.case (
			{ \osx }, { false },
			{ \linux }, { true } );
		strips = 0!8;
		channels = false!8;
		this.midiInit();
		Tones.initClass( midiChan );
		white = WhiteTone();
		serial = SerialTone();
		melodic = Melody();

		6.do{ | i |
			OSCresponderNode( net, ( "/ch" ++ ( i+1 ) ).asSymbol, {
				| time, responder, message |
				[ i, messages[ i ], message[ 1 ] == 1 ].postln;
				channels[ i ] = message[ 1 ] == 1;
				lastTouched = Clock.seconds;
				melodic.resume
			} ).add };

		OSCresponderNode( net, '/ch7', {
			| time, responder, message |
			[ "ch7 expand", message ].postln;
			if( message[ 1 ] == 1, {
				melodic.expand;
				melodic.melody.postln;
			} );
			channels[ 6 ] = message[ 1 ] == 1;
			lastTouched = Clock.seconds;
			melodic.resume
		} ).add;

		OSCresponderNode( net, '/ch8', {
			| time, responder, message |
			[ "ch8 prune", message ].postln;
			if( message[ 1 ] == 1, {
				melodic.prune;
				melodic.melody.postln;
			} );
			channels[ 7 ] = message[ 1 ] == 1;
			lastTouched = Clock.seconds;
			melodic.resume
		} ).add;

		OSCresponderNode( net, '/cont',
			{ | time, responder, message |
					strips[ message[ 1 ].asInt - 1 ] = message[ 2 ];
			 } ).add;
		Routine( {
			loop{
			if( channels[ 0 ], {
				Tones.tempo = strips[ 0 ]**3 } );
			if( channels[ 1 ], {
				Tones.phraseShorten = strips[ 1 ] } );
			if( channels[ 2 ], {
				Tones.stutter =  strips[ 2 ];
				}, {
				Tones.stutter = 0;
				} );
			if( channels[ 3 ], {
				Tones.amplitude = 1 - strips[ 3 ] } );
			if( channels[ 4 ], {
				Tones.accident( strips[ 4 ] ) } );
			if( channels[ 5 ], {
				Tones.chord( strips[ 5 ] ) } );
				if( Clock.seconds - lastTouched > 7, { melodic.pause } );
			0.05.wait } } ).play;
		melodic.play;
	}

	*midiInit {
		
		var uid;
		var touched = 0!8;
		if( testing, {
			MIDIClient.init( 1, 1 );
			uid = 1048576;
			midiChan = MIDIOut( 0, uid );
			midiChan.latency_( 0 );
		}, {
			MIDIClient.init;
			uid = MIDIClient.destinations[ 0 ].uid;
			midiChan = MIDIOut( 0, uid );
		} );
		midiChan.connect( uid );
		Melody.initClass( midiChan );
	}
}

+ Main {
	*run{
		Mg.run;
	}
}

